package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.HysYuyue;
import com.ruoyi.system.domain.OutPutExcel;
import com.ruoyi.system.service.IHysYuyueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * zzController
 *
 * @author ruoyi
 * @date 2025-09-25
 */
@RestController
@RequestMapping("/system/yuyue")
public class HysYuyueController extends BaseController {

    @Autowired
    private IHysYuyueService hysYuyueService;

    /**
     *设计一个本函数的全局变量， HysYuyue类，用来储存list传过来的参数，每次list变化就会覆盖，只保存最后的结果。该变量传到
     *  public void export(HttpServletResponse response, HysYuyue hysYuyue) 里面当做参数，1
     *设计
     */
    private HysYuyue lastListParams;

    /**
     * 查询zz列表，1
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(HysYuyue hysYuyue) {
        startPage();
        List<HysYuyue> list = hysYuyueService.selectHysYuyueList(hysYuyue);
        // 保存最新的查询参数到全局变量中
        this.lastListParams = hysYuyue;
        return getDataTable(list);
    }


    /**
     * 导出zz列表
     */
    @Log(title = "zz", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HysYuyue hysYuyue) {
        // 如果传入的hysYuyue参数为空，则使用lastListParams作为参数
        HysYuyue exportParams = (hysYuyue == null) ? this.lastListParams : hysYuyue;
        List<OutPutExcel> list = hysYuyueService.export(exportParams);
        ExcelUtil<OutPutExcel> util = new ExcelUtil<OutPutExcel>(OutPutExcel.class);
        util.exportExcel(response, list, "预约数据");
    }

    /**
     * 获取zz详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hysYuyueService.selectHysYuyueById(id));
    }

    /**
     * 新增zz
     */
    @Log(title = "zz", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HysYuyue hysYuyue) {
        return toAjax(hysYuyueService.insertHysYuyue(hysYuyue));
    }

    /**
     * 修改zz
     */
    @Log(title = "zz", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HysYuyue hysYuyue) {
        return toAjax(hysYuyueService.updateHysYuyue(hysYuyue));
    }

    /**
     * 删除zz
     */
    @Log(title = "zz", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('system:yuyue:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hysYuyueService.deleteHysYuyueByIds(ids));
    }


    /**
      * 统计每个科室的预约次数
      *
      * @return 每个科室的预约次数集合
      */
     @GetMapping("/countByDepartment")
     public AjaxResult countYuyueByDepartment() {
         return success(hysYuyueService.countYuyueByDepartment());
     }

      /**
       * 统计每个科室的预约的审批中次数
       *
       * @return 每个科室的预约的审批中次数集合
       */
      @GetMapping("/countByDepartmentAndState1")
      public AjaxResult countYuyueByDepartmentAndState1() {
          return success(hysYuyueService.countYuyueByDepartmentAndState1());
      }

      /**
       * 统计每个科室的预约的通过次数
       *
       * @return 每个科室的预约的通过次数集合
       */
      @GetMapping("/countByDepartmentAndState2")
      public AjaxResult countYuyueByDepartmentAndState2() {
          return success(hysYuyueService.countYuyueByDepartmentAndState2());
      }

      /**
       * 统计每个科室的预约的拒绝次数
       *
       * @return 每个科室的预约的拒绝次数集合
       */
      @GetMapping("/countByDepartmentAndState3")
      public AjaxResult countYuyueByDepartmentAndState3() {
      return success(hysYuyueService.countYuyueByDepartmentAndState3());
      }


    /**
     * 根据传过来的状态列表，统计每个科室的预约的次数
     *
     * @return 每个科室的预约的次数集合
     */
     @GetMapping("/countByDepartmentAndState4")
     public AjaxResult countYuyueByDepartmentAndState4(String[] status) {
         return success(hysYuyueService.countYuyueByDepartmentAndState4(status));
     }


      /**
       * 根据传过来的startDate和 endDate，统计这个区间内的会议日期
       * @param startDate 开始日期
       * @param endDate 结束日期
       * @return 这个区间内的会议日期集合
       */
      @GetMapping("/tongjiriqi")
      public AjaxResult tongjiriqi(LocalDate startDate, LocalDate endDate) {
          return success(hysYuyueService.tongjiriqi(startDate, endDate));
      }


    /**
     * 查询所有的待审批的预约
     *
     * @return 所有的待审批的预约集合
     */
    @GetMapping("/pendingAudit")
    public AjaxResult selectAllPendingAudit() {
        return success(hysYuyueService.selectAllPendingAudit());
    }


     /**
      * 根据前端传过来的id,status值，管理员用来审批待审批预约
      * @param hysYuyue 待审批预约实体类
      * @return 操作结果
      */
     @Log(title = "zz", businessType = BusinessType.UPDATE)
     @PreAuthorize("@ss.hasPermi('system:yuyue:audit')")
     @PutMapping("/audit")
     public AjaxResult audit(@RequestBody HysYuyue hysYuyue) {
         return toAjax(hysYuyueService.audit(hysYuyue));
     }


      /**
       * 统计每个取消的预约的拒绝次数，取消的状态码为4
       * @return 每个科室的取消预约的拒绝次数集合
       */
      @GetMapping("/countQuxiaoState4")
      public AjaxResult countQuxiaoState4() {
          return success(hysYuyueService.countQuxiaoState4());
      }


}
